local danshou = fk.CreateSkill {
  name = "ls__danshou",
  tags = { "Turn" }
}

Fk:loadTranslationTable{
  ["lsfa__zhuran"] = "朱然",
  ["#lsfa__zhuran"] = "孤磐未裂",
  ["designer:lsfa__zhuran"] = "朱苦力zzccll",
  ["zhuran_endnote"] = "严防死守，全面抗敌，坚决打赢防控关键战！",

  ["ls__danshou"] = "胆守",
  ["#ls__danshou_throw"] = "胆守",
  ["#ls__danshou-invoke"] = "胆守：你可翻面并摸两张牌",
  [":ls__danshou"] = "一名角色的出牌阶段开始时，若你的武将牌正面向上，你可摸两张牌并翻面；一名角色的出牌阶段结束时，若你的武将牌背面向上，你可移去区域内和中央区共计四张花色各不同的牌并翻面。",
  ["#ls__danshou-prompt"] = "胆守：你可移去区域内和中央区内共计四张花色各不同的牌并翻面",

  ["$ls__danshou1"] = "干戈可守江东地，肝胆独耀万世明！",
  ["$ls__danshou2"] = "守土何须十万兵，胆气足以据江城！",
  ["~lsy__zhuran"] = "力微胆气衰，难守江东地。",
}

local DIY = require "packages/diy_utility/diy_utility"

danshou:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

Fk:addPoxiMethod{
  name = "lvshi__danshou",
  prompt = "#ls__danshou-prompt",
  card_filter = function(to_select, selected, data)
    local s = Fk:getCardById(to_select).suit
    if #selected > 0 then
      for _, id in ipairs(selected) do
        if Fk:getCardById(id).suit == s then return false end
      end
    end
    return true
  end,
  feasible = function(selected, data, extra_data)
    return #selected == 4
  end,
}

danshou:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(danshou.name) and target.phase == Player.Play and player.faceup
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, danshou.name)
    player:turnOver()
  end,
})

danshou:addEffect(fk.EventPhaseEnd, {
  --anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(danshou.name) and target.phase == Player.Play and not player.faceup
  end,
  on_cost = function(self, event, target, player, data)
    if player.faceup then
      return player.room:askToSkillInvoke(player, { skill_name = danshou.name, prompt = "#ls__danshou-invoke" })
    else
      local danshou_cards = {{player.general, table.simpleClone( player:getCardIds("hej"))}}
      if #DIY.getCenterCards(player.room) > 0 then
        table.insert(danshou_cards, {"@$CenterArea", DIY.getCenterCards(player.room)})
      end
      local cards = player.room:askToPoxi(player, {
        poxi_type = "lvshi__danshou",
        data = danshou_cards,
        cancelable = true,
      })
      if #cards > 0 then
        event:setCostData(self, {ids = cards})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event:getCostData(self) and event:getCostData(self).ids and #event:getCostData(self).ids > 0 then
      local ids = event:getCostData(self).ids
      local cards = table.filter(ids, function (id)
        return table.contains(player:getCardIds("hej"), id)
      end)
      if #cards > 0 then
        player.room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, danshou.name, "", true, player)
      end
      DIY.removeCenterCards(player, ids)
    end
    player:turnOver()
  end,
})

return danshou